package com.ruby.bigtable.project.nginx;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class LogTest {

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Jan", "01");
		map.put("Feb", "02");
		map.put("Mar", "03");
		map.put("Apr", "04");
		map.put("May", "05");
		map.put("Jun", "06");
		map.put("Jul", "07");
		map.put("Aug", "08");
		map.put("Sept", "09");
		map.put("Oct", "10");
		map.put("Nov", "11");
		map.put("Dec", "12");

		// TODO Auto-generated method stub
		String content = "220.112.121.213 - - [08/Jan/2018:20:59:46 +0800] \"POST /vpn HTTP/1.1\" 200 31 \"http://www.companyclub.cn/vpn\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 OPR/49.0.2725.64\"";
		StringTokenizer tklStringTokenizer = new StringTokenizer(content, " ");
		int index = 0;
		String resultString = "";
		while (tklStringTokenizer.hasMoreTokens()) {
			String rawString = tklStringTokenizer.nextToken();
			switch (index) {
			case 0:
				// 用户ip
				String remote_addr = rawString;
				resultString += remote_addr + ",";
				
				AddressUtils addressUtils = new AddressUtils();
				// 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信
				String ip = remote_addr;
				String address = "";
				try {
					address = addressUtils.getAddresses("ip=" + ip, "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(address);
				// 输出结果为：广东省,广州市,越秀区
				break;
			case 3:
				rawString = rawString.substring(1);
				String dayString = rawString.split(":")[0];
				String leftString = rawString.substring(dayString.length() + 1);
				String[] dayStrings = dayString.split("/");
				dayString = dayStrings[2] + map.get(dayStrings[1])
						+ dayStrings[0];

				String visit_time = dayStrings[2] + "-"
						+ map.get(dayStrings[1]) + "-" + dayStrings[0] + " "
						+ leftString;
				resultString += dayString + "," + visit_time + ",";
				break;
			case 5:
				String typeString = rawString.substring(1);
				resultString += typeString + ",";
				break;
			case 6:
				String urlString = rawString.substring(1);
				resultString += urlString + ",";
				break;
			case 8:
				resultString += rawString + ",";
				break;
			case 9:
				resultString += rawString + ",";
				break;
			case 10:
				String referString = rawString.replaceAll("\"", "");
				resultString += referString;
				break;
			}
			index++;
		}
		System.out.println(resultString);
	}

}
